<template>
  <el-radio-group v-model="vRadio" v-bind="$attrs">
    <el-radio v-for="item in radioOptions" :key="item[sign.id]" :value="sign.value?item[sign.value]:item[sign.id]">{{item[sign.label]}}</el-radio>
  </el-radio-group>
</template>

<script lang="ts" setup>
import { onMounted, computed } from 'vue'
import { RadioPropsInterface } from './types'

// model 也要类型检查
type ModelType = {
  [key: string]: any;
};

const { store, sign } = defineProps<RadioPropsInterface>()
const model = defineModel<ModelType>({required: true})
// v-model 名称之间别有空格
const emit = defineEmits(['update:modelValue'])

// 请求
onMounted(async ()=>{
  if (store.autoload) {
    await store.searchData()
  }
})

const vRadio = computed({
  get(){
    if(typeof model.value === 'object'){
      return model.value[sign.value] || model.value[sign.id]
    } else {
      return model.value
    }
  },
  set(val: string){
    const res = store.view.value.data.find(item => {
      if(item[sign.value] === val){
        return item
      }
    })
    emit('update:modelValue', res)
  }
})
// 获取值
const radioOptions = computed(()=>{
  return store.view.value.data || []
})

</script>